﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GU.WebERP.Model;
using System.Collections;
using GU.WebERP.Core.Utils;
using System.Configuration;
using YGOP.ESB.Log;

namespace GU.WebERP.DAL
{
    public class DAL_Fct_User : JnsFramework.Core.TableOperating
    {
        readonly JnsFramework.Core.ExecCustomizeSQL _exec = new JnsFramework.Core.ExecCustomizeSQL();
        readonly Model_Fct_User _modelFct_User = new Model_Fct_User();

        public DAL_Fct_User()
        {
            base.DBKey = "GU.ERPDb";
        }

        protected override JnsFramework.Common.IModel GetTableModel()
        {
            return _modelFct_User;
        }


        public List<Model_Fct_User> GetAll()
        {
            string sql = string.Format(@"SELECT [UserId]
                                          ,[UserName]
                                          ,[MenuRights]
                                          ,[IsAdmin]
                                          ,[Remark]
                                          ,[Disabled]
                                          ,[Rec_CreateTime]
                                          ,[Rec_CreateBy]
                                          ,[Rec_ModifyTime]
                                          ,[Rec_ModifyBy]
                                      FROM [dbo].[Fct_User] where disabled=0");
            var dt = _exec.Query(sql);
            return ConvertDtHelper<Model_Fct_User>.GetModelList(dt);
        }

        public Model_Fct_User GetSingle(int id)
        {
            string sql = string.Format(@"SELECT [UserId]
                                          ,[UserName]
                                          ,[MenuRights]
                                          ,[IsAdmin]
                                          ,[Remark]
                                          ,[Disabled]
                                          ,[Rec_CreateTime]
                                          ,[Rec_CreateBy]
                                          ,[Rec_ModifyTime]
                                          ,[Rec_ModifyBy]
                                      FROM [dbo].[Fct_User] where disabled=0 and UserId=@UserId");
            Hashtable ht = new Hashtable();
            ht.Add("@UserId", id);
            var dt = _exec.Query(sql, ht);
            return ConvertDtHelper<Model_Fct_User>.GetModel(dt);
        }

        public Model_Fct_User Login(string name, string pwd)
        {
            string sql = @"SELECT [UserId]
                                ,[UserName]
                                ,[MenuRights]
                                ,[IsAdmin]
                                ,[Remark]
                                ,[Disabled]
                                ,[Rec_CreateTime]
                                ,[Rec_CreateBy]
                                ,[Rec_ModifyTime]
                                ,[Rec_ModifyBy]
                            FROM [dbo].[Fct_User]
                            where [Disabled]=0 and UserName=@UserName and PassWord=@PassWord";
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("@UserName", name);
                ht.Add("@PassWord", pwd);
                var dt = _exec.Query(sql, ht);
                return ConvertDtHelper<Model_Fct_User>.GetModel(dt);
            }
            catch (Exception ex)
            {
                LogManager.WriteDbError("用户登录", sql, name + pwd, ex);
            }
            return null;
        }
    }
}
